package com.ruoyi.client.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.client.domain.SysMedical;
import com.ruoyi.client.domain.SysPurOrderGoods;
import com.ruoyi.client.service.ISysMedicalService;
import com.ruoyi.client.service.ISysPurOrderGoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.client.domain.SysPurchaseOrder;
import com.ruoyi.client.service.ISysPurchaseOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购订单 Controller
 * 
 * @author ruoyi
 * @date 2022-09-14
 */
@RestController
@RequestMapping("/system/order")
public class SysPurchaseOrderController extends BaseController
{
    @Autowired
    private ISysPurchaseOrderService sysPurchaseOrderService;
    @Autowired
    private ISysPurOrderGoodsService sysPurOrderGoodsService;
    @Autowired
    private ISysMedicalService sysMedicalService;

    /**
     * 查询采购订单 列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysPurchaseOrder sysPurchaseOrder)
    {
        startPage();
        List<SysPurchaseOrder> list = sysPurchaseOrderService.selectSysPurchaseOrderList(sysPurchaseOrder);
        return getDataTable(list);
    }

    /**
     * 导出采购订单 列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "采购订单 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPurchaseOrder sysPurchaseOrder)
    {
        List<SysPurchaseOrder> list = sysPurchaseOrderService.selectSysPurchaseOrderList(sysPurchaseOrder);
        ExcelUtil<SysPurchaseOrder> util = new ExcelUtil<SysPurchaseOrder>(SysPurchaseOrder.class);
        util.exportExcel(response, list, "采购订单 数据");
    }

    /**
     * 获取采购订单 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{purId}")
    public AjaxResult getInfo(@PathVariable("purId") Long purId)
    {
        SysPurOrderGoods sysPurOrderGoods = new SysPurOrderGoods();
        sysPurOrderGoods.setPurId(purId);
        final List<SysPurOrderGoods> purOrderGoods = sysPurOrderGoodsService.selectSysPurOrderGoodsList(sysPurOrderGoods);
        final SysPurchaseOrder sysPurchaseOrder = sysPurchaseOrderService.selectSysPurchaseOrderByPurId(purId);
        sysPurchaseOrder.setSysPurOrderGoodsList(purOrderGoods);
        return AjaxResult.success(sysPurchaseOrder);
    }

    /**
     * 新增采购订单 
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "采购订单 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPurchaseOrder sysPurchaseOrder)
    {
        sysPurchaseOrderService.insertSysPurchaseOrder(sysPurchaseOrder);
        List<SysPurchaseOrder> orderList = sysPurchaseOrderService.selectSysPurchaseOrderList(sysPurchaseOrder);
        Long purId = orderList.get(0).getPurId();
//        遍历商品，存入采购订单商品表
        for (SysMedical item: sysPurchaseOrder.getList()){
            SysMedical sysMedical = sysMedicalService.selectSysMedicalByMedId(item.getMedId());
//            增加库存操作
            if (sysMedical.getInventory() == null){
                sysMedical.setInventory(item.getPurGoodNum());
            }else {
                sysMedical.setInventory(item.getPurGoodNum()+sysMedical.getInventory());
            }
            sysMedicalService.updateSysMedical(sysMedical);
            SysPurOrderGoods sysPurOrderGoods = new SysPurOrderGoods();
            sysPurOrderGoods.setMedId(item.getMedId());
            sysPurOrderGoods.setPurId(purId);
            sysPurOrderGoods.setPurGoodUnit(item.getUnit());
            sysPurOrderGoods.setMedName(item.getMedName());
            sysPurOrderGoods.setPurGoodPrice(item.getPurGoodPrice());
            sysPurOrderGoods.setPurGoodNum(item.getPurGoodNum());
            sysPurOrderGoodsService.insertSysPurOrderGoods(sysPurOrderGoods);
        }
        System.out.println(sysPurchaseOrder);
        return toAjax(1);
    }

    /**
     * 修改采购订单 
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "采购订单 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPurchaseOrder sysPurchaseOrder)
    {
        return toAjax(sysPurchaseOrderService.updateSysPurchaseOrder(sysPurchaseOrder));
    }

    /**
     * 删除采购订单 
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "采购订单 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{purIds}")
    public AjaxResult remove(@PathVariable Long[] purIds)
    {
        return toAjax(sysPurchaseOrderService.deleteSysPurchaseOrderByPurIds(purIds));
    }
}
